package net.huashitong.supervision.gongjian.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.gongjian.entity.ProjectFeedBackEntity;
import net.huashitong.supervision.gongjian.filter.ProjectFeedBackFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProjectFeedBackDao extends StringDao<ProjectFeedBackEntity> {


    public List<ProjectFeedBackEntity> getList(ProjectFeedBackFilter filter)
    {

        GenericQuery query = createQuery("obj");

        if(ObjectUtils.isNotBlank(filter.getProjectId()))
        {
            query.append(" and projectId=:projectId").setParam("projectId",filter.getProjectId());
        }

        return query.listResult(filter);

    }

    public ProjectFeedBackEntity getFeedback(String projectId,String taskId,int projectType,String userId)
    {
        GenericQuery query = createQuery("obj");
        query.append(" and projectId=:projectId").setParam("projectId",projectId);
        query.append(" and taskId=:taskId").setParam("taskId",taskId);
        query.append(" and projectType=:projectType").setParam("projectType",projectType);
        query.append(" and createUser=:createUser").setParam("createUser",userId);
        return query.singleResult();

    }

    public List<ProjectFeedBackEntity> getFeebackByName(String projectId, String taskId, int projectType, String createName)
    {
        GenericQuery query = createQuery("obj");
        query.append(" and projectId=:projectId").setParam("projectId",projectId);
        query.append(" and taskId=:taskId").setParam("taskId",taskId);
        query.append(" and projectType=:projectType").setParam("projectType",projectType);
        if(StringUtils.isNotBlank(createName))
        {
            query.append(" and createName=:createName").setParam("createName",createName);
        }
        return query.listResult();

    }

    /**
     * 当期填报的条数
     * @param projectId
     * @param taskValid
     * @param projectType
     * @return
     */
    public int getCurrnetFeedbackCount(String projectId, int taskValid, int projectType){

        String hql = "select z " +
                "     from  ProjectFeedBackEntity as z ,ProjectTaskEntity as g " +
                "      where z.taskId=g.sysId ";
        GenericQuery query =create(hql);
        query.append(" and g.projectType=:projectType").setParam("projectType",projectType);
        query.append(" and g.projectId=:projectId").setParam("projectId",projectId);
        query.append(" and g.valid=:valid").setParam("valid",taskValid);

        int count = query.count();

        return count;
    }

    /**
     * 当期填报的列表
     * @param projectId
     * @param taskValid
     * @param projectType
     * @return
     */
    public List<ProjectFeedBackEntity> getCurrnetFeedbackList(String projectId, int taskValid, int projectType,String createName){

        String hql = "select z " +
                "     from  ProjectFeedBackEntity as z ,ProjectTaskEntity as g " +
                "      where z.taskId=g.sysId ";
        GenericQuery query =create(hql);
        query.append(" and g.projectType=:projectType").setParam("projectType",projectType);
        query.append(" and g.projectId=:projectId").setParam("projectId",projectId);
        query.append(" and g.valid=:valid").setParam("valid",taskValid);
        if(StringUtils.isNotBlank(createName))
        {

            query.append(" and z.createName=:createName").setParam("createName",createName);
        }

        return query.listResult();

    }



}
